package UseIO.Practice;

import java.io.*;
import java.util.*;

public class UseBuffer {
    public static void main(String[] args) throws IOException{
        // 四种方式的拷贝比较
        practice03();
    }
    public static void practice01() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("8.内置模块/UseIO/a.txt"));
        String s;
        ArrayList<String> arr = new ArrayList<>();
        while ((s=br.readLine())!=null){
            arr.add(s);
        }
        //存数据
        Collections.sort(arr,(s1,s2)->{
          int i1 = Integer.parseInt(s1.split("\\.")[0]);
          int i2 = Integer.parseInt(s2.split("\\.")[0]);
          return i1-i2;
        });
        //写入
        BufferedWriter bw = new BufferedWriter(new FileWriter("8.内置模块/UseIO/sort.txt"));
        for (String string : arr) {
            bw.write(string);
            bw.newLine();
        }
        br.close();
        bw.close();
    }

    public static void practice02() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("8.内置模块/UseIO/a.txt"));
        String s;
        // TreeMap默认的排序
        TreeMap<Integer,String> treeMap = new TreeMap<>();
        while ((s=br.readLine())!=null){
            treeMap.put(Integer.parseInt(s.split("\\.")[0]),s);
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("8.内置模块/UseIO/sort02.txt"));
        Set<Map.Entry<Integer, String>> entries = treeMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            bw.write(entry.getValue());
            bw.newLine();
        }
        br.close();
        bw.close();
    }

    public static void practice03() throws IOException{
        BufferedReader br = new BufferedReader(new FileReader("8.内置模块/UseIO/b.txt"));
        String s = br.readLine();
        br.close();
        int count = Integer.parseInt(s);
        count++;
        if(count<=3){
            System.out.println("第"+count+"操作了");
        }else {
            System.out.println("只能免费搞三次，去注册吧");
            return;
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("8.内置模块/UseIO/b.txt"));
        bw.write(count+"");
        bw.close();
    }
}
